package com.example.testdemo.demos.web.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.testdemo.demos.web.Config.JwtInfo;
import com.example.testdemo.demos.web.Config.Result;
import com.example.testdemo.demos.web.Config.ResultEnum;
import com.example.testdemo.demos.web.Dao.MenuItemDao;
import com.example.testdemo.demos.web.Entity.MenuItem;
import com.example.testdemo.demos.web.Service.MenuItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MenuItemServiceImpl implements MenuItemService {
    @Resource
    private MenuItemDao menuItemDao;

    /**
     * 查询菜单接口
     * @param menu
     * @return
     */
    @Override
    public Result getMenuItem(MenuItem menu, JwtInfo jwtInfo){
        Result result =new Result();
        try {
            LambdaQueryWrapper<MenuItem>lqw=new LambdaQueryWrapper<>();
            lqw.eq(MenuItem::getMenuCode,menu.getMenuCode());
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("菜单查询成功");
            if (jwtInfo.getPass()){
                List<MenuItem> itemList =menuItemDao.selectList(lqw);
                List<MenuItem> resList =new ArrayList<>();
                for (MenuItem item:itemList){
                    for (String item2:jwtInfo.getPurview()){
                        if (item2.contains(item.getMenuPurview())){
                            resList.add(item);
                        }
                    }
                }
                result.setData(resList);
            }else {
                lqw.eq(MenuItem::getMenuPurview,"ordinary");
                List<MenuItem> itemList =menuItemDao.selectList(lqw);
                result.setData(itemList);
            }

        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.UNKNOWNERROR.getData());
            result.setData(e);
        }
        return result;
    }
}
